package com.java.concurrency.stampedlocktest;

import java.util.concurrent.Exchanger;

public class ExchangeVo1 {

    public static void main(String[] args) {
        Exchanger<String> exchanger=new Exchanger();
        new Thread(()->{
            System.out.println("i m t1");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                String hello_t2 = exchanger.exchange("hello t2");
                System.out.println("t1获得返回值"+hello_t2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("t1 end");
        }).start();

        new Thread(()->{
            System.out.println("i m t2");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                String ok_t1 = exchanger.exchange("ok t1");
                System.out.println("t2获得返回值"+ok_t1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2 end");

        }).start();
    }
}
